# coding: utf8
# try something like

import datetime
now = datetime.datetime.now()

from makan import *

def call():
    return service()
    
def test():
    from xmlrpclib import ServerProxy
    s = ServerProxy('http://127.0.0.1:8000/makan/getNotify/call/xmlrpc')
    p = s.xNotify(18)
    return  dict(p = p)
    
def coreGetNotify(uid): 
    user = db(db.users.id == uid).select().first()
    result = {}
    if user:
        for f in user.following :
           lastcheckin = db(db.checkins.user == f).select( orderby = ~db.checkins.datetime).first()
           
           followee = db(db.users.id  == f).select().first()
           place =  db(db.places.checkins.contains(lastcheckin.id)).select().first()
       
           result[str(f)] = {"placename" : place.name , "First_name": followee.first_name , "Lastname": followee.last_name }
          
        return dict(  f = result)
    
    
@service.xmlrpc
def xNotify(uid):
    msg = jsonDictOfDicts(coreGetNotify(uid))
    return msg

@auth.requires_login()
def notify():
    uid = auth.user.id   
    return coreGetNotify(uid)
